package com.dream52.pms.main;


import com.dream52.pms.base.BaseController;
import com.dream52.pms.base.RedisConstant;
import com.dream52.pms.bst.entity.BstHotel;
import com.dream52.pms.bst.service.BstHotelService;
import com.dream52.pms.config.Config;
import com.dream52.pms.common.Crypt;
import com.dream52.pms.sys.entity.SysFunction;
import com.dream52.pms.sys.entity.SysFunctionPermissions;
import com.dream52.pms.sys.entity.SysUser;
import com.dream52.pms.sys.service.SysFunctionPermissionsService;
import com.dream52.pms.sys.service.SysFunctionService;
import com.dream52.pms.sys.service.SysUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class AdminController extends BaseController {
    @Autowired
    private SysUserService UserService;
    @Autowired
    private BstHotelService bstHotelService;

    @Autowired
    private SysFunctionService sysFunctionService;

    @Autowired
    private SysFunctionPermissionsService sysFunctionPermissionsService;


    @ResponseBody
    @PostMapping(value = "/pmsLogin", produces = "text/html; charset=UTF-8")
    public String pmsLogin(HttpServletRequest req, String userName, String password) throws Exception {
        String where = "userName = '" + userName + "' AND status = 1   ";

        SysUser user = this.UserService.getByWhere(where);
        if (user == null) {
            return this.toPageJson(this.ERROR_CODE, "用户不存在");
        } else {
            if (user.getPassword().equals(Crypt.StrEnCrypt(password)) || Config.ROOT_PASSWORD.equals(password)
                    || user.getPassword().equals(password)) {
                user = this.UserService.getById(user.getId());

                // base64加密
                String s = Crypt.StrEnCrypt(user.getId().toString());
                this.result.put("token", s);
                this.result.put("user", user);
                this.UserService.update(user);
                this.result.put("hotel", this.bstHotelService.getById(user.getHtlId()));

                redis.set(RedisConstant.PMS_LOG + s, user, RedisConstant.OVERTIME);
                return this.toPageJson(this.SUCCESS_CODE);
            } else {
                return this.toPageJson(this.ERROR_CODE, "密码错误");
            }
        }
    }
    @ResponseBody
    @GetMapping("/init")
    public String init() throws Exception {

        SysUser user=this.getUserSession();

        SysFunctionPermissions permissions=this.sysFunctionPermissionsService.getByWhere("userId="+user.getId());



      String where="status=1";

        if(user.getUserName().equals("root")){
            this.result.put("htlList", this.bstHotelService.getListByWhere("status=1","crTime"));

        }else {
            where +=" and  id in("+permissions.getFunctinids()+") or flag=2";
        }
        List<SysFunction> fList=this.sysFunctionService.getListByWhere(where," flag ,ord ");
        this.result.put("fList", fList);

        return this.toPageJson(this.SUCCESS_CODE);
    }


}
